package com.duanjc.mergemd.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * [线程池配置类]
 *
 * @author : [duanjiangcheng]
 * @version : [v1.0]
 * @createTime : [2022/11/24 16:50]
 */
@Configuration
@EnableAsync
public class ThreadConfig {

    @Value("${threadPool.corePoolSize:10}")
    private int corePoolSize;

    @Value("${threadPool.maxPoolSize:30}")
    private int maxPoolSize;

    @Value("${threadPool.queueCapacity:1000}")
    private int queueCapacity;

    @Value("${threadPool.keepAliveSeconds:60}")
    private int keepAliveSeconds;


    @Bean
    public ThreadPoolTaskExecutor executor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //配置核心线程数
        executor.setCorePoolSize(corePoolSize);
        //配置最大线程数
        executor.setMaxPoolSize(maxPoolSize);
        //配置队列大小
        executor.setQueueCapacity(queueCapacity);
        //线程的名称前缀
        executor.setThreadNamePrefix("Executor-");
        //线程活跃时间（秒）
        executor.setKeepAliveSeconds(keepAliveSeconds);
        //等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        //设置拒绝策略
        //executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //执行初始化
        executor.initialize();
        return executor;
    }
}
